package com.simplegovern.dict.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import com.simpleframe.web.action.CrudActionSupport;
import com.simplegovern.dict.Dict;
import com.simplegovern.dict.DictItem;
import com.simplegovern.dict.service.DictItemService;

@Scope("prototype")
@Namespace("/dictItem")
@Action("/dictItem/dictItem")
public class DictItemAction extends CrudActionSupport  {
	@Autowired
	private DictItemService dictItemService;
	private DictItem dictItem;
	private Dict dict;

	@Override
	public String main(){
		return MAIN;
	}
	
	@Override
	public String grid() throws Exception {
		System.out.println(dict);
		page = dictItemService.getPage(getSearcher(),dict);
		return GRID;
	}
	
	@Override
	public String create() throws Exception {
		setSaveMode();
		dictItem = new DictItem();
		
		return EDIT;
	}
	
	@Override
	public String edit() throws Exception {
		setUpdateMode();
		dictItem = dictItemService.getById(dictItem.getId());
		return EDIT;
	}
	
	@Override
	public String save() throws Exception {
		dictItem.setTypeId(dict.getId());
		dictItemService.create(dictItem);
		return success("添加成功");
	}
	
	public String update() throws Exception {
		
		dictItemService.update(dictItem);
		return success("修改成功");
	}
	
	public void prepareUpdate() throws Exception {
		dictItem = dictItemService.getById(getId());
	}
	
	public void validator() throws Exception {
		System.out.println("执行验证");
	}
	
	@Override
	public String delete() throws Exception {
		dictItemService.deletes(ids);
		addMessage("删除成功");
		return null;
	}
	
	//setters and getters
	
	public void setDictItem(DictItem dictItem) {
		this.dictItem = dictItem;
	}
	
	public DictItem getDictItem() {
		return dictItem;
	}
	
	public Dict getDict() {
		return dict;
	}
	
	public void setDict(Dict dict) {
		this.dict = dict;
	}
}
